package com.ohs.ohsoa.utils.auth;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.auth0.jwt.JWT;

/*
* @author whf
* @version 1.0
*/
public class TokenUtil {

    public static String getTokenUserId() {
        String token = getRequest().getHeader("token");// 从 http 请求头中取出 token
        if(token != null) {
            return JWT.decode(token).getAudience().get(0);
        }else {
        	return null;
        }
    }

    /**
     * 获取request
     *
     * @return  requestAttributes.getRequest()
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }
}